<template>
	<view>
		<view class="shezhi_t">
			<view class="shezhi_img">
				<image :src="my_avatar" mode=""></image>
			</view>
			<text class="wcl-3ch m-t3" @click="upfile">更换头像</text>
		</view>
		<view class="shezhi_x m-q3">
			<view class="shezhi_xm">
				<text class="m-l2">我的名字：</text>
				<input class="xgmz m-r2" type="text" v-model="cons.name" value="">
			</view>
			<view class="shezhi_xm">
				<text class="m-l2">我的手机号：</text>
				<view class="f">
					<text class="m-r2">{{cons.phone}}</text>
					<text class="xiugi m-r2" @tap="tz()">修改</text>
				</view>
			</view>
<!-- 			<view class="shezhi_xm">
				<text class="m-l2">修改密码：</text>
				<text class="xiugi m-r2" @tap="toForgetPwd()">修改</text>
			</view> -->
			<view class="tuichu m-t6">
				<button type="warn" @click="tuichu">退出登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		pathToBase64,
		base64ToPath
	} from '../../js_sdk/mmmm-image-tools/index.js'
	export default {
		data() {
			return {
				cons: [],
				pics: [],
				my_avatar: ''
			}

		},
		created() {
			let that = this
			that.pyReq(that.$http.getMyInfo,{},function(res){
				that.cons = res.result
				that.my_avatar = res.result.avatar
			});
		},
		mounted() {
			
		},
		onNavigationBarButtonTap(e) {
			uni.switchTab({
				url: './index'
			})
		},
		methods: {
			toForgetPwd(){
				uni.navigateTo({
					url: '../index/logo/logosj'
				})
			},
			tz() {
				uni.navigateTo({
					url: './xiugaishouji'
				})
			},
			/* 上传头像 */
			upfile() {
				let that = this
				console.log(that.my_avatar)
				let headers = {
					// 'content-type': 'application/x-www-form-urlencoded',
					'token': uni.getStorageSync('token')
				}
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						let imgsArr = that.pics.concat(res.tempFilePaths);
						let upImgs = imgsArr.map((val, index) => {
							return {
								name: 'image' + index,
								uri: val
							};
						});
						uni.showLoading({
							title: '上传图片',
							mask: false
						});
						// 从本地获取图片完毕 上传
						uni.uploadFile({
							url: 'http://'+uni.getStorageSync("baseUrl")+'/uploadImageUniAPP',
							files: upImgs,
							name: 'file',
							header: headers,
							formData: {
								image: upImgs.url
							},
							success: (res) => {
								console.log("成功");
								let data = JSON.parse(res.data);
								let jiequ = data.result.substring(0, data.result.length - 1)
								console.log(jiequ);
								that.my_avatar = 'http://'+uni.getStorageSync("baseUrl") + '/img/' + jiequ;
								console.log(that.my_avatar);
								that.pyReq(that.$http.changeAvatar,{'avatar': jiequ},function(res){
									
								});
								
								// uni.request({
								// 	url: that.$http.changeAvatar,
								// 	method: 'POST',
								// 	header: {
								// 		'content-type': 'application/x-www-form-urlencoded',
								// 		'token': uni.getStorageSync('token')
								// 	},
								// 	data: {
								// 		'avatar': jiequ
								// 	},
								// 	success: (res) => {
								// 		console.log(res)
								// 	},
								// 	fail: (err) => {
								// 		console.log(err)
								// 	}
								// })
								// console.log(that.imgsStr += data.result)
							},
							fail: (e) => {
								console.log("网络请求fail");
							},
							complete: () => {
								console.log("网络请求complete");
								uni.hideLoading();
								uni.showToast({
									title: '上传成功！',
									icon: 'none'
								});
							}
						});
					}
				});
			},
			tuichu() {
				let that = this
				that.pyReq(that.$http.logout,{},function(res){
					uni.clearStorageSync();
					uni.reLaunch({
						url: '/pages/index/logo/logo'
					})
				});
				that.pyReq(that.$http.updateCid, {}, function (res) {});
				// let headers = {
				// 	'content-type': 'application/x-www-form-urlencoded',
				// 	'token': uni.getStorageSync('token')
				// }
				// uni.request({
				// 	url: that.$http.logout,
				// 	method: 'POST',
				// 	header: headers,
				// 	success: (res) => {
				// 		console.log(res)
				// 		uni.clearStorageSync();
				// 		uni.showToast({
				// 			icon: 'none',
				// 			title: res.data.msg,
				// 			success: function(res) {
				// 				uni.reLaunch({
				// 					url: '/pages/index/logo/logo'
				// 				})
				// 			}
				// 		})
				// 	},
				// 	fail: (err) => {
				// 		console.log(err)
				// 	}
				// })
			}
		}
	}
</script>

<style>
	.xiugi {
		border: 1upx solid #B31610;
		border-radius: 15upx;
		padding: 0 10upx;
		color:#B31610;
		font-size: 26upx;
	}

	.shezhi_xm {
		display: flex;
		justify-content: space-between;
		padding: 40upx 0;
		border-bottom: 1upx solid #B31610;
	}

	.xgmz {
		text-align: right;
		font-size: 26upx;
	}

	.shezhi_t {
		display: flex;
		align-items: center;
		flex-direction: column;
		margin-top: 80upx;
	}

	.shezhi_img {
		width: 190upx;
		height: 190upx;
		border-radius: 50%;
		background-color: #B31610;
		overflow: hidden;
	}

	.shezhi_img image {
		width: 190upx;
		height: 190upx;
	}
</style>
